package com.csw.android.player.view.controller

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.animation.AlphaAnimation
import android.widget.FrameLayout
import android.widget.ImageView
import androidx.core.view.isVisible
import com.csw.android.dev_utils.ex.ViewEx.gone
import com.csw.android.dev_utils.ex.ViewEx.visible
import com.csw.android.dev_utils.utils.ImageLoader

class PreviewView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null
) : FrameLayout(context, attrs) {
    private val cover = ImageView(context).apply {
        scaleType = ImageView.ScaleType.CENTER_CROP
    }
    private val previewShowAnim = AlphaAnimation(0f, 1f).apply {
        duration = 1300
    }
    private val previewHideAnim = AlphaAnimation(1f, 0f).apply {
        duration = 1300
    }

    init {
        addView(
            cover,
            LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)
        )
    }

    fun show() {
        visible()
        startAnimation(previewShowAnim)
    }

    fun hide() {
        gone()
        startAnimation(previewHideAnim)
    }

    fun loadPreviewImage(path: String?) {
        ImageLoader.loadImage(context, cover, path)
    }

    fun isShowing() = isVisible

    override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
        return true
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        return true
    }
}